(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.popup.repaymentView.viewController", viewController);
	viewController.$inject = [ "$scope", "$q", "dialogsModule", "$uibModalInstance", "$filter", "layoutConfig", "validatorConfig", "gridService", "authService"
	                        , "settlementSummaryService", "settlementPaymentService", "glEntryService" ];
	function viewController($scope, $q, dialogsModule, $uibModalInstance, $filter, layoutConfig, validatorConfig, gridService, authService,
			 settlementSummaryService, settlementPaymentService, glEntryService) {
		var ctrl = this, settlement = {};
		ctrl.dialogsModule = dialogsModule;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.setlDetailsConfig = layoutConfig.formConfigs.setlDetailView.tableConfigs.setlDetails.columnDefs;
		ctrl.isDetailsPay = function(item){
			return settlementSummaryService.isDetailsPay(item);
		};
		
		ctrl.isDetailsReceive = function(item){
			return settlementSummaryService.isDetailsReceive(item);
		};
		
		ctrl.init = function() {	
			ctrl.amdIntSchdListGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.amdIntSchd.tableConfigs.table, null);
			ctrl.amdIntSchdListGridOptions.data = "vm.loanAmd.intSchdNewList";
			
			ctrl.orgIntSchdListGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.orgIntSchd.tableConfigs.table, null);
			ctrl.orgIntSchdListGridOptions.data = "vm.loanAmd.intSchdOldList";
			
			ctrl.amdPastdueListGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.amdPastdue.tableConfigs.table, null);
			ctrl.amdPastdueListGridOptions.data = "vm.loanAmd.pastdueSchdNewList";
			
			ctrl.orgPastdueListGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.orgPastdue.tableConfigs.table, null);
			ctrl.orgPastdueListGridOptions.data = "vm.loanAmd.pastdueSchdOldList";
			
			ctrl.txnSetlInsts = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.txnSetlInsts.tableConfigs.txnSetlInsts, null);
			ctrl.txnSetlInsts.data = "vm.settlement.txnSetlInsts";
			var defer1 = $q.defer(), defer2 = $q.defer();
			settlementSummaryService.getFxRateTypes().then(function(fxRateTypes){
				settlement.fxRateTypes = fxRateTypes;
				defer1.resolve();
			});
			settlementPaymentService.settlementPaymentOptions().then(function(map) {
				settlement.deliveryTypeOptions = map.deliveryTypeOptions;
				settlement.currencyOptions = map.currencyOptions;
				settlement.allPartyTypeOptions = map.partyTypeOptions;
				settlement.swiftCaseList = map.swiftCaseList;
				defer2.resolve();
			});
			($q.all({
				defer1 : defer1.promise,
				defer2 : defer2.promise
			})).then(function(){
				authService.viewInit({amdId : ctrl.dialogsModule.params.amdId}).then(function(response) {
					if (response.data) {
						ctrl.loanAmd = response.data.loanAmd;
						settlement = angular.merge(settlement, response.data.settlement);
						angular.extend(ctrl, settlementPaymentService.getPayDetailsConfig(settlement, layoutConfig));
						settlementSummaryService.initData(settlement);
						ctrl.payPartyConfig = settlementPaymentService.initData(settlement, layoutConfig);
						processTxnSetlInsts(settlement);
						getGlEntryList(ctrl, settlement);
						ctrl.settlement = settlement;
						ctrl.layoutConfig = layoutConfig;
						resetPastdue();
					}
				});
			});
		};
		
		function getGlEntryList(ctrl, settlement){
			glEntryService.getGlentryList($scope, layoutConfig, settlement.settlementInfo.parentId, "CLS:" + settlement.settlementInfo.parentType).then(function(response){
				ctrl.glEntryListConfig = response.glEntryListConfig;
				ctrl.glEntryList = response.glEntryList;
			});
		};
		
		function processTxnSetlInsts(settlement){
			if(settlement.txnSetlInsts){
				angular.forEach(settlement.txnSetlInsts, function(data, index, array){
					data.paycodeName = data.paycode ? data.paycode.substr(7) : "";
					data.itemType_desc = $filter("translate")("label.settlement.init." + data.itemType.toLowerCase());
				});
			}
		}
		
		function resetPastdue(){
			if (ctrl.loanAmd.pastdueSchdNewList && ctrl.loanAmd.pastdueSchdNewList.length > 0) {
				for (var i = 0; i < ctrl.loanAmd.pastdueSchdNewList.length; i++) {
					if (i == 0) {
						ctrl.loanAmd.pastdueSchdNewList[i].pastdueTenorFrom = 1;
						continue;
					}
					var prePastdueTenor = ctrl.loanAmd.pastdueSchdNewList[i - 1].pastdueTenor;
					var pastdueTenorFrom = arithService.add(prePastdueTenor, 1);
					ctrl.loanAmd.pastdueSchdNewList[i].pastdueTenorFrom = pastdueTenorFrom;
				}
			}
			
			if (ctrl.loanAmd.pastdueSchdOldList && ctrl.loanAmd.pastdueSchdOldList.length > 0) {
				for (var i = 0; i < ctrl.loanAmd.pastdueSchdOldList.length; i++) {
					if (i == 0) {
						ctrl.loanAmd.pastdueSchdOldList[i].pastdueTenorFrom = 1;
						continue;
					}
					var prePastdueTenor = ctrl.loanAmd.pastdueSchdOldList[i - 1].pastdueTenor;
					var pastdueTenorFrom = arithService.add(prePastdueTenor, 1);
					ctrl.loanAmd.pastdueSchdOldList[i].pastdueTenorFrom = pastdueTenorFrom;
				}
			}
		}
		//settlement payment
		ctrl.swiftTypeCallBack = function(result){
			settlementPaymentService.swiftTypeCallBack(ctrl.settlement, layoutConfig, result);
		};
		
		ctrl.partyCountryChanged = function(payParty){
			settlementPaymentService.partyCountryChanged(ctrl.settlement, payParty);
		};
		
		ctrl.partyStateChanged = function(payParty){
			settlementPaymentService.partyStateChanged(ctrl.settlement, payParty);
		};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};
	}
})(angular);